const { EventEmitter } = require('events');

const flow = new EventEmitter();

/**
 * 设置模块依赖启动
 * @param {string[]} eventNameList 依赖的模块列表
 * @param {string} emitName 自身模块名
 */
exports.dependenceOn = function (eventNameList, callback) {
  const depList = [];
  for (const eventName of eventNameList) {
    depList.push(new Promise((resolve) => {
      flow.on(eventName, resolve);
    }));
  }
  Promise.all(depList).then(() => {
    callback();
  });
};

/**
 * 模块加载完成事件
 * @param {string} emitName 
 */
exports.moduleLoad = function (emitName) {
  flow.emit(emitName);
};
